local GuideSecondUI = class("GuideSecondUI", BaseUI)

local POSY = 110

function GuideSecondUI:ctor()
    self.uiIndex = GAME_UI.UI_GUIDESECOND
end

function GuideSecondUI:init()
    local guidesecondBgImg = self.root:getChildByName("guidesecond_bg_img")
    local guidesecondNode = guidesecondBgImg:getChildByName("guidesecond_node")
    self:adaptUI(guidesecondBgImg, guidesecondNode)

    --local winsize = cc.Director:getInstance():getWinSize()
    self.node1 = guidesecondNode:getChildByName("node_1")
    self.node2 = guidesecondNode:getChildByName("node_2")
    self.node3 = guidesecondNode:getChildByName("node_3")
    self.closeBtn = self.root:getChildByName("close_btn")
end

function GuideSecondUI:playAnimation()
    local winsize = cc.Director:getInstance():getWinSize()
    local guidesecondBgImg = self.root:getChildByName("guidesecond_bg_img")
    local spine = GlobalApi:createSpineByName("guide_zhuizhu", "spine/guide_zhuizhu/guide_zhuizhu", 1)
    spine:setPosition(cc.p(0, 110))
    guidesecondBgImg:addChild(spine)
    spine:setAnimation(0, "run", true)
    spine:runAction(cc.Sequence:create(cc.MoveTo:create(5, cc.p(winsize.width/2, 110)), cc.CallFunc:create(function ()
        GuideMgr:finishCurrGuide()
    end)))

    local posx, posy = self.node1:getPosition()
    self.node1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.MoveBy:create(20, cc.p(-1140, 0)), cc.CallFunc:create(function ()
        self.node1:setPosition(cc.p(posx, posy))
    end))))
    self.node2:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.MoveBy:create(12, cc.p(-1140, 0)), cc.CallFunc:create(function ()
        self.node2:setPosition(cc.p(posx, posy))
    end))))
    self.node3:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.MoveBy:create(4, cc.p(-1140, 0)), cc.CallFunc:create(function ()
        self.node3:setPosition(cc.p(posx, posy))
    end))))

    local guidetextConf = GameData:getConfData("local/guidetext")
    self.root:runAction(cc.Sequence:create(cc.DelayTime:create(0.5), cc.CallFunc:create(function ()
        local dialog1 = cc.Sprite:create("uires/ui/battle/bg_talk_5.png")
        local dialogSize1 = dialog1:getContentSize()
        local label1 = cc.Label:createWithTTF("", "font/gamefont1.TTF", 38)
        label1:setPosition(cc.p(dialogSize1.width/2, dialogSize1.height/2 + 4))
        label1:setTextColor(COLOR_TYPE.WHITE)
        label1:enableOutline(cc.c4b(63, 132, 178, 255), 2)
        label1:setString(guidetextConf["GUIDE_TEXT_35"].text)
        if guidetextConf["GUIDE_TEXT_35"].soundRes ~= "0" then
            AudioMgr.playEffect("media/guide/" .. guidetextConf["GUIDE_TEXT_35"].soundRes, false)
        end
        dialog1:addChild(label1)
        dialog1:setPosition(cc.p(250, 150))
        spine:addChild(dialog1)
        dialog1:setScale(0)
        dialog1:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, 1), cc.DelayTime:create(1.5), cc.CallFunc:create(function ()
            dialog1:removeFromParent()
        end)))
    end), cc.DelayTime:create(1.5), cc.CallFunc:create(function ()
        local dialog2 = cc.Sprite:create("uires/ui/battle/bg_talk_6.png")
        local dialogSize2 = dialog2:getContentSize()
        local label2 = cc.Label:createWithTTF("", "font/gamefont1.TTF", 38)
        label2:setPosition(cc.p(dialogSize2.width/2, dialogSize2.height/2 + 4))
        label2:setTextColor(COLOR_TYPE.WHITE)
        label2:enableOutline(cc.c4b(218, 79, 32, 255), 2)
        label2:setString(guidetextConf["GUIDE_TEXT_36"].text)
        if guidetextConf["GUIDE_TEXT_36"].soundRes ~= "0" then
            AudioMgr.playEffect("media/guide/" .. guidetextConf["GUIDE_TEXT_36"].soundRes, false)
        end
        dialog2:addChild(label2)
        dialog2:setPosition(cc.p(140, 150))
        spine:addChild(dialog2)
        dialog2:setScale(0)
        dialog2:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, 1), cc.DelayTime:create(1), cc.CallFunc:create(function ()
            dialog2:removeFromParent()
        end)))
    end)))
end



function GuideSecondUI:showLiuBei()
    local winsize = cc.Director:getInstance():getWinSize()
    local ani = GlobalApi:createSpineByName("guide_rengdan", "spine_lossless/guide_rengdan/guide_rengdan", 2)
    ani:setLocalZOrder(2)
    ani:setName("guide_rengdan")
    ani:setPosition(cc.p(winsize.width/2, winsize.height/2))
    self.root:addChild(ani)
    GuideMgr:finishCurrGuide()
end

function GuideSecondUI:showDragonEgg()
    local winsize = cc.Director:getInstance():getWinSize()
    local ani = self.root:getChildByName("guide_rengdan")
    if ani == nil then
        ani = GlobalApi:createSpineByName("guide_rengdan", "spine_lossless/guide_rengdan/guide_rengdan", 2)
        ani:setLocalZOrder(2)
        ani:setName("guide_rengdan")
        ani:setPosition(cc.p(winsize.width/2, winsize.height/2))
        self.root:addChild(ani)
    end
    ani:registerSpineEventHandler(function (event)
        if event.animation == "idle01" then
            -- 粒子
            local particle = cc.ParticleSystemQuad:create("particle/guide_egg_point.plist")
            particle:setPosition(cc.p(winsize.width/2, winsize.height/2))
            particle:setName("guide_egg_point")
            self.root:addChild(particle)
            local guidesecondBgImg = self.root:getChildByName("guidesecond_bg_img")
            local eggBtn = guidesecondBgImg:getChildByName("egg_btn")
            eggBtn:setTouchEnabled(true)
            eggBtn:setContentSize(winsize)
            ani:setAnimation(0, "idle02", true)
            GuideMgr:finishCurrGuide()
        end
    end, sp.EventType.ANIMATION_COMPLETE)
    ani:setAnimation(0, "idle01", false)
end

function GuideSecondUI:showGuideTreasure()
    local ani = self.root:getChildByName("guide_rengdan")
    local particle = self.root:getChildByName("guide_egg_point")
    if ani then
        ani:removeFromParent()
    end
    if particle then
        particle:removeFromParent()
    end
    local guideTreasureUI = require("script/app/ui/guide/guideui/guidetreasure").new()
    guideTreasureUI:showUI()
    GuideMgr:finishCurrGuide()
end

function GuideSecondUI:showLiuGuanZhang()
    self:createLiuGuanZhang()
    local winsize = cc.Director:getInstance():getWinSize()
    local liubei = self.root:getChildByName("guide_liubei")
    local guanyu = self.root:getChildByName("guide_guanyu")
    local zhangfei = self.root:getChildByName("guide_zhangfei")

    self.liubei:setPosition(cc.p(winsize.width/2 - 200, POSY))
    self.guanyu:setPosition(cc.p(winsize.width + 200, POSY))
    self.zhangfei:setPosition(cc.p(winsize.width/2, POSY))
    self.liubei:setAnimation(0, "idle", true)
    self.guanyu:setAnimation(0, "run", true)
    self.zhangfei:setAnimation(0, "idle", true)
    self.guanyu:setScaleX(-1)
    self.guanyuDialog:setScaleX(-1)
    local guidetextConf = GameData:getConfData("local/guidetext")
    self.guanyu:runAction(cc.Sequence:create(cc.MoveTo:create(2, cc.p(winsize.width/2 + 200, POSY)), cc.CallFunc:create(function ()
        self.guanyu:setAnimation(0, "idle", true)
        local dialog = cc.Sprite:create("uires/ui/battle/bg_talk_3.png")
        local dialogSize = dialog:getContentSize()
        local label = cc.Label:createWithTTF("", "font/gamefont1.TTF", 38)
        label:setMaxLineWidth(100)
        label:setPosition(cc.p(dialogSize.width/2 - 4, dialogSize.height/2 + 4))
        label:setTextColor(COLOR_TYPE.WHITE)
        label:enableOutline(cc.c4b(63, 132, 178, 255), 2)
        label:setString(guidetextConf["GUIDE_TEXT_28"].text)
        if guidetextConf["GUIDE_TEXT_28"].soundRes ~= "0" then
            AudioMgr.playEffect("media/guide/" .. guidetextConf["GUIDE_TEXT_28"].soundRes, false)
        end
        dialog:addChild(label)
        dialog:setPosition(cc.p(0, 150))
        self.guanyu:addChild(dialog)
        dialog:setScale(0)
        dialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, -1, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            dialog:removeFromParent()
        end)))
    end), cc.DelayTime:create(3), cc.CallFunc:create(function ()
        self.liubeiDialog:setVisible(true)
        self.liubeiEmoticon:setTexture("uires/ui/guide/emoticon_small_4.png")
        local size = self.liubeiEmoticon:getContentSize()
        self.liubeiDialogBg:setContentSize(cc.size(size.width + 40, 60))
        self.liubeiEmoticon:setPosition(cc.p(0, 42))
        self.liubeiDialog:setScale(0)
        self.liubeiDialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            self.liubeiDialog:setVisible(false)
        end)))
    end), cc.DelayTime:create(3), cc.CallFunc:create(function ()
        local dialog = cc.Sprite:create("uires/ui/battle/bg_talk_4.png")
        local dialogSize = dialog:getContentSize()
        local label = cc.Label:createWithTTF("", "font/gamefont1.TTF", 38)
        label:setMaxLineWidth(100)
        label:setPosition(cc.p(dialogSize.width/2 - 4, dialogSize.height/2 + 4))
        label:setTextColor(COLOR_TYPE.WHITE)
        label:enableOutline(cc.c4b(218, 79, 32, 255), 2)
        label:setString(guidetextConf["GUIDE_TEXT_29"].text)
        if guidetextConf["GUIDE_TEXT_29"].soundRes ~= "0" then
            AudioMgr.playEffect("media/guide/" .. guidetextConf["GUIDE_TEXT_29"].soundRes, false)
        end
        dialog:addChild(label)
        dialog:setPosition(cc.p(0, 150))
        self.liubei:addChild(dialog)
        dialog:setScale(0)
        dialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            dialog:removeFromParent()
        end)))
    end), cc.DelayTime:create(1), cc.CallFunc:create(function ()
        GuideMgr:finishCurrGuide()
    end)))
end

function GuideSecondUI:showLiuGuanZhang2()
    self:createLiuGuanZhang()
    local winsize = cc.Director:getInstance():getWinSize()
    self.liubei:setPosition(cc.p(winsize.width/2 - 200, POSY))
    self.zhangfei:setPosition(cc.p(winsize.width/2, POSY))
    self.guanyu:setPosition(cc.p(winsize.width/2 + 200, POSY))
    self.liubei:setAnimation(0, "idle", true)
    self.guanyu:setAnimation(0, "idle", true)
    self.zhangfei:setAnimation(0, "idle", true)
    self.guanyu:setScaleX(-1)
    self.guanyuDialog:setScaleX(-1)
    self.closeBtn:setVisible(true)
    GuideMgr:finishCurrGuide()
end

function GuideSecondUI:showCatchLiuBei()
    if self.liubei == nil then
        self:createLiuGuanZhang()
        local winsize = cc.Director:getInstance():getWinSize()
        self.liubei:setPosition(cc.p(winsize.width/2 - 200, POSY))
        self.zhangfei:setPosition(cc.p(winsize.width/2, POSY))
        self.guanyu:setPosition(cc.p(winsize.width/2 + 200, POSY))
        self.liubei:setAnimation(0, "idle", true)
        self.guanyu:setAnimation(0, "idle", true)
        self.zhangfei:setAnimation(0, "idle", true)
        self.guanyu:setScaleX(-1)
        self.guanyuDialog:setScaleX(-1)
        self.closeBtn:setVisible(true)
    end
    self.root:runAction(cc.Sequence:create(cc.DelayTime:create(1), cc.CallFunc:create(function ()
        self.liubeiDialog:setVisible(true)
        self.guanyuDialog:setVisible(true)
        self.zhangfeiDialog:setVisible(true)
        self.liubeiEmoticon:setTexture("uires/ui/guide/emoticon_small_3.png")
        self.guanyuEmoticon:setTexture("uires/ui/guide/emoticon_small_3.png")
        self.zhangfeiEmoticon:setTexture("uires/ui/guide/emoticon_small_3.png")
        local size = self.liubeiEmoticon:getContentSize()
        self.liubeiDialogBg:setContentSize(cc.size(size.width + 40, 60))
        self.guanyuDialogBg:setContentSize(cc.size(size.width + 40, 60))
        self.zhangfeiDialogBG:setContentSize(cc.size(size.width + 40, 60))
        self.liubeiEmoticon:setPosition(cc.p(0, 42))
        self.guanyuEmoticon:setPosition(cc.p(0, 42))
        self.zhangfeiEmoticon:setPosition(cc.p(0, 42))
        self.liubeiDialog:setScale(0)
        self.guanyuDialog:setScale(0)
        self.zhangfeiDialog:setScale(0)
        self.liubeiDialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            self.liubeiDialog:setVisible(false)
        end)))
        self.guanyuDialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, -1, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            self.guanyuDialog:setVisible(false)
        end)))
        self.zhangfeiDialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            self.zhangfeiDialog:setVisible(false)
        end)))
    end), cc.DelayTime:create(2), cc.CallFunc:create(function ()
        self.liubei:registerSpineEventHandler(function (event)
            if event.animation == "idle" then
                self.liubei:setAnimation(0, "diaozou", false)
            end
        end, sp.EventType.ANIMATION_COMPLETE)
    end), cc.DelayTime:create(2), cc.CallFunc:create(function ()
        self.guanyuEmoticon:setTexture("uires/ui/guide/emoticon_small_1.png")
        self.zhangfeiEmoticon:setTexture("uires/ui/guide/emoticon_small_1.png")
        self.guanyuDialog:setVisible(true)
        self.zhangfeiDialog:setVisible(true)
        self.guanyuDialog:setScale(0)
        self.zhangfeiDialog:setScale(0)
        self.guanyuDialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, -1, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            self.guanyuDialog:setVisible(false)
        end)))
        self.zhangfeiDialog:runAction(cc.Sequence:create(cc.ScaleTo:create(0.2, 1), cc.DelayTime:create(2), cc.CallFunc:create(function ()
            self.zhangfeiDialog:setVisible(false)
        end)))
    end), cc.DelayTime:create(3), cc.CallFunc:create(function ()
        self.guanyu:runAction(cc.MoveBy:create(2, cc.p(1140, 0)))
        self.zhangfei:runAction(cc.MoveBy:create(2, cc.p(1140, 0)))
        self.guanyu:setScaleX(1)
        self.guanyu:setAnimation(0, "run", true)
        self.zhangfei:setAnimation(0, "run2", true)
    end), cc.DelayTime:create(2), cc.CallFunc:create(function ()
        GuideMgr:finishCurrGuide()
    end)))
end

function GuideSecondUI:createLiuGuanZhang()
    local winsize = cc.Director:getInstance():getWinSize()
    local liubei = GlobalApi:createSpineByName("guide_liubei", "spine/guide_liubei/guide_liubei", 1)
    local guanyu = GlobalApi:createSpineByName("guide_guanyu", "spine/guide_guanyu/guide_guanyu", 1)
    local zhangfei = GlobalApi:createSpineByName("guide_zhangfei", "spine/guide_zhangfei/guide_zhangfei", 1)
    self.liubei = liubei
    self.guanyu = guanyu
    self.zhangfei = zhangfei
    self.root:addChild(liubei)
    self.root:addChild(guanyu)
    self.root:addChild(zhangfei)

    -- 对话框
    local liubeiDialog = cc.Sprite:create("uires/ui/guide/dialog_jiao.png")
    local guanyuDialog = cc.Sprite:create("uires/ui/guide/dialog_jiao.png")
    local zhangfeiDialog = cc.Sprite:create("uires/ui/guide/dialog_jiao.png")
    self.liubeiDialog = liubeiDialog
    self.guanyuDialog = guanyuDialog
    self.zhangfeiDialog = zhangfeiDialog
    liubeiDialog:setAnchorPoint(cc.p(0.5, 0))
    guanyuDialog:setAnchorPoint(cc.p(0.5, 0))
    zhangfeiDialog:setAnchorPoint(cc.p(0.5, 0))
    liubeiDialog:setPosition(cc.p(0, 100))
    guanyuDialog:setPosition(cc.p(0, 100))
    zhangfeiDialog:setPosition(cc.p(0, 100))
    liubei:addChild(liubeiDialog)
    guanyu:addChild(guanyuDialog)
    zhangfei:addChild(zhangfeiDialog)
    liubeiDialog:setVisible(false)
    guanyuDialog:setVisible(false)
    zhangfeiDialog:setVisible(false)
    -- 对话框背景
    local liubeiDialogBg = ccui.ImageView:create("uires/ui/guide/bg_dialog2.png")
    local guanyuDialogBg = ccui.ImageView:create("uires/ui/guide/bg_dialog2.png")
    local zhangfeiDialogBG = ccui.ImageView:create("uires/ui/guide/bg_dialog2.png")
    self.liubeiDialogBg = liubeiDialogBg
    self.guanyuDialogBg = guanyuDialogBg
    self.zhangfeiDialogBG = zhangfeiDialogBG
    liubeiDialogBg:setScale9Enabled(true)
    guanyuDialogBg:setScale9Enabled(true)
    zhangfeiDialogBG:setScale9Enabled(true)
    liubeiDialogBg:setAnchorPoint(cc.p(0.5, 0))
    guanyuDialogBg:setAnchorPoint(cc.p(0.5, 0))
    zhangfeiDialogBG:setAnchorPoint(cc.p(0.5, 0))
    liubeiDialogBg:setPosition(cc.p(0, 11))
    guanyuDialogBg:setPosition(cc.p(0, 11))
    zhangfeiDialogBG:setPosition(cc.p(0, 11))

    liubeiDialog:addChild(liubeiDialogBg)
    guanyuDialog:addChild(guanyuDialogBg)
    zhangfeiDialog:addChild(zhangfeiDialogBG)
    -- 表情
    local liubeiEmoticon = cc.Sprite:create()
    local guanyuEmoticon = cc.Sprite:create()
    local zhangfeiEmoticon = cc.Sprite:create()
    self.liubeiEmoticon = liubeiEmoticon
    self.guanyuEmoticon = guanyuEmoticon
    self.zhangfeiEmoticon = zhangfeiEmoticon
    liubeiDialog:addChild(liubeiEmoticon)
    guanyuDialog:addChild(guanyuEmoticon)
    zhangfeiDialog:addChild(zhangfeiEmoticon)

    local jiao1 = cc.Sprite:create("uires/ui/guide/dialog_jiao.png")
    local jiao2 = cc.Sprite:create("uires/ui/guide/dialog_jiao.png")
    local jiao3 = cc.Sprite:create("uires/ui/guide/dialog_jiao.png")
    jiao1:setAnchorPoint(cc.p(0, 0))
    jiao2:setAnchorPoint(cc.p(0, 0))
    jiao3:setAnchorPoint(cc.p(0, 0))
    liubeiDialog:addChild(jiao1)
    guanyuDialog:addChild(jiao2)
    zhangfeiDialog:addChild(jiao3)
end

return GuideSecondUI